import { download,downloadByConfig,downloadPLM } from '@/api/business/file'
import { Message } from 'element-ui'


/**
 * 下载文件
 * @param {文件id} fileId
 */
export function downloadFileId(fileId, filename) {
    download(fileId).then(res => {
        if(res.size>0){
            var blob = new Blob([res], {
                type: 'application/octet-stream;charset=UTF-8'
            })

            var downloadElement = document.createElement('a')
            var href = window.URL.createObjectURL(blob) // 创建下载的链接

            downloadElement.style.display = 'none'
            downloadElement.href = href
            downloadElement.download = decodeURI(filename) // 下载后文件名
            document.body.appendChild(downloadElement)
            downloadElement.click() // 点击下载
            document.body.removeChild(downloadElement) // 下载完成移除元素
            window.URL.revokeObjectURL(href)
        }else{
            Message({
                message:"找不到对应的文件！",
                type:'error'
            })
        }
    })
}

// /**
//  * 下载文件
//  * @param {任务ID} taskId
//  */
// export function downloadPLMFile(taskId, filename) {
    
//     downloadPLM(taskId).then(res => {
        
//         if(res.size>0){
//             var blob = new Blob([res], {
//                 type: 'application/octet-stream;charset=UTF-8'
//             })

//             var downloadElement = document.createElement('a')
//             var href = window.URL.createObjectURL(blob) // 创建下载的链接

//             downloadElement.style.display = 'none'
//             downloadElement.href = href
//             //downloadElement.download = decodeURI(filename) // 下载后文件名
//             document.body.appendChild(downloadElement)
//             downloadElement.click() // 点击下载
//             document.body.removeChild(downloadElement) // 下载完成移除元素
//             window.URL.revokeObjectURL(href)
//         }else{
//             Message({
//                 message:"找不到对应的文件！",
//                 type:'error'
//             })
//         }
//     })
// }


/**
 * 下载文件
 * @param {配置项} configKey
 */
 export function downloadByConfigKey(configKey, filename) {
    downloadByConfig(configKey).then(res => {
        if(res.size>0){
            var blob = new Blob([res], {
                type: 'application/octet-stream;charset=UTF-8'
            })

            var downloadElement = document.createElement('a')
            var href = window.URL.createObjectURL(blob) // 创建下载的链接

            downloadElement.style.display = 'none'
            downloadElement.href = href
            downloadElement.download = decodeURI(filename) // 下载后文件名
            document.body.appendChild(downloadElement)
            downloadElement.click() // 点击下载
            document.body.removeChild(downloadElement) // 下载完成移除元素
            window.URL.revokeObjectURL(href)
        }else{
            Message({
                message:"找不到对应的文件！",
                type:'error'
            })
        }
    })
}



/**
 * 下载文件的方法
 * @param {文件id} fileId
 * @param {文件名称} filename
 */
export function downLoadFileById(fileId, filename) {
    download(fileId).then(res => {
        if (res.size > 0) {
            var suf = filename.split('.').pop().toLowerCase()
            let contextType = {}
            //设置文件下载的类型
            if (suf == "pdf") {
                contextType.type = "application/pdf"
            } else if (suf == "xls") {
                contextType.type = "application/vnd.ms-excel"
            } else if (suf == "xlsx") {
                contextType.type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
            } else if (suf == "doc") {
                contextType.type = "application/msword"
            } else if (suf == "docx") {
                contextType.type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
            } else if (suf == "png") {
                contextType.type = "image/png"
            } else if (suf == ".jpeg") {
                contextType.type = "image/jpeg"
            } else if (suf == ".zip") {
                contextType.type = "application/x-zip-compressed"
            } else {
                contextType.type = "application/octet-stream"
            }
            const blob = new Blob([res], contextType);
            // const fileName = res.headers["content-disposition"].split(";")[1].split("filename=")[1];
            // const fileName = row.physicsName
            if ('download' in document.createElement("a")) {
                const link = document.createElement("a");
                link.download = filename;
                link.style.display = 'none';
                link.href = URL.createObjectURL(blob);
                document.body.appendChild(link);
                link.click();
                URL.revokeObjectURL(link.href);
                document.body.removeChild(link);
            } else {
                navigator.msSaveBlob(blob, filename);
            }
        } else {
            Message({
                message:"找不到对应的文件！",
                type:'error'
            })
        }

    })
}
